import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/Home')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/home/search/Search'),
  },
  {
    path: '/search-list',
    component: () => import('@/views/home/search/Search-list')
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import('@/views/detail/Detail'),
    meta: {
      hiddenTabBar: true
    }
  },
  {
    path: '/category',
    name: 'category',
    component: () => import('@/views/category/Category')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/views/cart/Cart')
  },
  {
    path: '/profile',
    name: 'profile',
    component: () => import('@/views/profile/user/Index'),
    redirect: '/profile/user',
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/profile/User')
      },
      {
        path: 'my',
        name: 'my',
        component: () => import('@/views/profile/user/My'),
        meta: {
          auth: true
        }
      },
      {
        path: 'location',
        name: 'location',
        component: () => import('@/views/profile/location/Index'),
        redirect: '/profile/location/all_locations',
        children: [
          {
            path: 'all_locations',
            name: 'all_locations',
            component: () => import('@/views/profile/location/Location'),
            meta: {
              auth: true,
              hiddenTabBar: true
            },
          },
          {
            path: 'add',
            name: 'add',
            component: () => import('@/views/profile/location/Add'),
            meta: {
              auth: true,
              hiddenTabBar: true
            },
          },
          {
            path: 'edit',
            name: 'edit',
            component: () => import('@/views/profile/location/Edit'),
            meta: {
              auth: true,
              hiddenTabBar: true
            },
          }
        ],
        meta: {
          auth: true
        }
      },
      {
        path: 'allorders',
        name: 'allorders',
        component: () => import('@/views/profile/order/AllOrders')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/profile/user/Login')
  },
  {
    path: '/login_pwd',
    name: 'login_pwd',
    component: () => import('@/views/profile/user/Loginpwd')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/profile/user/Register')
  },
  {
    path: '/order',
    name: 'order_index',
    component: () => import('@/views/pay/Index'),
    redirect: '/order/order_home',
    children: [
      {
        path: 'order_home',
        name: 'order_home',
        component: () => import('@/views/pay/Order'),
        meta: {
          auth: true,
          hiddenTabBar: true
        }
      },
      {
        path: 'paysuccess',
        name: 'paysuccess',
        component: () => import('@/views/pay/PaySuccess'),
        meta: {
          auth: true,
          hiddenTabBar: true
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    if (!store.state.user.LoginState && !localStorage.getItem('userToken')) {
      return next('/login')
    }
    next()
  } else {
    next()
  }
})

export default router
